package com.abu.crms.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.abu.crms.config.common.Resp;
import com.abu.crms.pojo.Permission;
import com.abu.crms.service.PermissionService;
import com.abu.crms.vo.PermissionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("getPermissionList")
    public Resp queryAllPermission() {
        List<Permission> permissionList = this.permissionService.list();
        if (CollectionUtil.isEmpty(permissionList)) {
            return Resp.error().msg("数据为空");
        }
        return Resp.ok().data("permissionList", permissionList);
    }

    @GetMapping("searchByName")
    public Resp queryAllPermission(@RequestParam("key") String key) {
        List<Permission> permissionList = this.permissionService.list(new QueryWrapper<Permission>().like("ps_name", key));
        return Resp.ok().data("permissionList", permissionList);
    }

    @GetMapping("queryPermissionLevel01And02ByRole")
    public Resp queryPermissionTree(@RequestParam("roleId") Long roleId) {
        List<PermissionVO> permissionVoList = this.permissionService.queryPermissionTree(roleId);
        List<Long> idList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(permissionVoList)) {
            permissionVoList.stream().forEach(permissionVo -> {
                List<Permission> permissionList = permissionVo.getPermissionList();
                if (CollectionUtil.isNotEmpty(permissionVoList)) {
                    permissionList.forEach(permission -> {
                        idList.add(permission.getPsId());
                    });
                }
                idList.add(permissionVo.getPsId());
            });
        }
        return Resp.ok().data("idList", idList);
    }

    @GetMapping("queryAllPermissionList")
    public Resp queryAllPermissionList() {
        List<PermissionVO> permissionVoList = this.permissionService.queryAllPermissionList();
        if (CollectionUtil.isNotEmpty(permissionVoList)) {
            return Resp.ok().data("permissionVoList", permissionVoList);
        }
        return Resp.error().msg("数据为空");
    }

}

